/**
 * Created by Yu Jin Xian on 2016/11/25.
 */
var http = require('http'),
    fs = require('fs');

http.createServer(pathing).listen(3000);

console.log('Server started on http://localhost:3000; press Ctrl+C to terminate....');

function serverStaticFile(res, path, contentType, responseCode) {
    if (!responseCode) {
        responseCode = 200;
    }
    fs.readFile(__dirname + path, publicDir);

    function publicDir(err, data) {
        if (err) {
            res.writeHead(500, {'Content-Type': 'text/plain'});
            res.end('500 - Internal Error');
        } else {
            res.writeHead(responseCode, {'Content-Type': contentType});
            res.end(data);
        }
    }
}

function pathing(req, res) {
    var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
    switch (path) {
        case '':
            serverStaticFile(res,'/public/home.html','text/html');
            break;
        case '/about':
            serverStaticFile(res,'/public/about.html','text/html');
            break;
        case '/img/logo.png':
            serverStaticFile(res,'/public/img/logo.png','image/png');
            break;
        default :
            serverStaticFile(res,'/public/404.html','text/html');
            break;
    }
}